package textapp.business;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import textapp.connector.ConnectorMessage;
import textapp.connector.MessageCode;
import textapp.connector.model.UserModel;
import textapp.dao.UserDao;
import textapp.dao.model.User;

public class RegisterService {
	public User registerDevice(String identity, String name) {
		UserDao dao = new UserDao();
		User u = dao.findByIdentity(identity);
		if (u == null) {
			u = dao.create(name, identity, 0);
		} else {
			if (u.getName().equalsIgnoreCase(name) || u.getStatus() != 0) {
				dao.updateNameStatus(u, name, 0);
			}
		}
		// :TODO update name, status
		return u;
	}
	
	public User registerNonMemDevice(String identity, String name) {
		UserDao dao = new UserDao();
		User u = dao.findByIdentity(identity);
		if (u == null) {
			u = dao.create(name, identity, 1);
		}
		return u;
	}
	
	public List<User> pollContacts(List<String> identityList) {
		UserDao dao = new UserDao();
		return dao.getUsersFromIdentities(identityList);		
	}
	
	public boolean deleteUser(int userId) {
		return new UserDao().deleteUser(userId);
	}
	
	public boolean saveRegistrationId(int userId, String regisId) {
		return new UserDao().saveRegistrationId(userId, regisId);
	}
	
	public static void main(String[] args) { 
		
		//////////////////
		// Example of how Gson works
		/////////////////
		
//		User u = new RegisterService().registerDevice("+16505561111", "Kosin2");
//		Gson gson = new Gson();
//		String str = gson.toJson(u);
//		System.out.println(str);
//		
		Gson gson = new Gson();
		List<String> strList = new ArrayList<String>();
		strList.add("+2222");
		strList.add("+1234");		
		strList.add("+4444");
		System.out.println(gson.toJson(strList));
		
		String asdasd = gson.toJson(strList);
		
		Type strListType = new TypeToken<ArrayList<String>>(){}.getType();	 	
		ArrayList<String> contactList = gson.fromJson("[\"+2222\",\"+1234\",\"+4444\"]", strListType);
		
		for (String c: contactList) {
			System.out.println(c);
		}
		System.out.println(contactList);
		List<User> users = new RegisterService().pollContacts(contactList);
		for (User u : users) {
			System.out.println(u.getId()+", "+u.getIdentity()+", "+u.getStatus());
		}
//		ConnectorMessage m = new ConnectorMessage(MessageCode.SUCCESS, users);
//		String js = gson.toJson(m);
//		System.out.println(js);
//		
//		ConnectorMessage m2 = gson.fromJson(js, ConnectorMessage.class);
//		System.out.println(m2.getBody());
		
//		UserModel um = gson.fromJson(str, UserModel.class);
//		System.out.println(um.getId()+", "+um.getIdentity()+", "+um.getStatus());
		
//		List<String> strList = new ArrayList<String>();
//		strList.add("+2222");
//		strList.add("+1234");		
//		strList.add("+4444");
//		strList.add("+asasd");
//		List<User> users = new RegisterService().pollContacts(strList);
//		for (User u : users) {
//			System.out.println(u.getId()+", "+u.getIdentity()+", "+u.getStatus());
//		}
	}
}
